<?php 

$peer = ProductAlikePeer::getInstance();
$id = Zend_Controller_Front::getInstance()->getRequest()->getParam('id');
$select = $peer->select();
$select->where(ProductAlikePeer::PRODUCT_ID, $id);
$rowset = $peer->fetchAll($select);

$count = $rowset->count();
echo "<label><b>Похожие товары ($count):</b></label><br />";
foreach ($rowset as $row) {
	$product = ProductPeer::getInstance()->find($row->getAlikeId())->current();
	$url = $this->url(array('id' => $product->getId()));
	echo "
	<a href='$url'>{$product->getName()}</a>
	&nbsp;&nbsp;
	<a href='#' class='delete_alike' type='alike' row_id='{$product->getId()}'>[ x ]</a>
	<br />";
}


echo "<div class='container_alike'>
<label>Добавить похожий товар (по имени) <a href='#' class='add_alike' type='alike'>[ + ]</a></label>
";
$request = Zend_Controller_Front::getInstance()->getRequest();
$alikes = $request->getParam('alike', array());
$first = 1;
foreach ($alikes as $alike) {
	if ($first) {
		$first = 0;
		continue;
	}
	
	if (!$alike) continue;
	echo 
	"<div class='alike_element'>
		<input type='text' autocomplete='off' name='alike[]' class='alike_suggest' type='attendant' value='$alike' />
		<div class='suggest_result'></div>
	</div>";
}

echo "
	<div class='alike_element'>
		<input type='text' autocomplete='off' name='alike[]' class='alike_suggest' type='alike'/>
		<div class='suggest_result'></div>
	</div>

</div>";

?>


<script type='text/javascript'>

var $countOfPhotosAdded = 1;
$(function() {
	$(".add_alike").live("click", function() {
		var $self = $(this);
		var $label = $self.parent();
		var $element = $label.next();
		var $container = $label.parent();
		var $clone = $element.clone();
		$container.append($clone);
		$clone.find(':first').val('');

		return false;
	});


	$(".delete_alike").live("click", function() {
		if (!confirm("Вы уверены, что хотите удалить связанный товар ?")) return false;
		$self = $(this);
		var $url = '/admin/product/delete-alike/id/' + $self.attr('row_id') + '/type/' + $self.attr('type') + '/product_id/' + <?php echo $id; ?>;
		$.ajax({
			url: $url,
			type: 'post',
			dataType: 'json',
			success: function($data) {
				if ($data.result == 'success') {
					$self.prev().remove();
					$self.next().remove();
					$self.remove();
				} else {
					alert($data.message);
				}
			}
		});

		return false;
	});


	var suggestTimeout = null;
	$("input.alike_suggest").live('keyup', function() {
		if (suggestTimeout != null) {
			clearTimeout(suggestTimeout);
			suggestTimeout = null;
		}
		
		var $self = $(this);
		var $value = $self.val();
		if (!$value) return;

		var $data = 'type=' + $self.attr('type') + '&name=' + $value;
		suggestTimeout = setTimeout(function() {
			var $url = '/admin/product/suggest';
			$.ajax({ 
				url: $url, 
				type: 'post',
				data: $data,
				context: document.body,
				success: function(html) {
		        	if (html == '') {
		        		$(".suggest_result").hide();
			        	blinkField($self);
			        	return false;
		        	}

					var $result = $self.next();
		        	$result.html(html);
		        	$result.show();
		    	}
			});
		}, 650);
			
		return;
	});

	$("li.suggest_item").live('click', function() {
		var $self = $(this);

		var $result = $self.parent().parent();
		var $input = $result.prev();
		$input.val($self.text() + ":" +$self.attr('rel'));
		$result.hide();
	});
	
});




</script>

<style type='text/css'>
.suggest_result {
	text-align: left;
	position: absolute;
	width: 300px;
	height: 200px;
	overflow-y: scroll;
	overflow-x: hidden;
	background-color: white;
	border: 2px black solid;
	display: none;
}

.suggest_item:hover, .suggest_item_street:hover, .suggest_item_city:hover {
	cursor: pointer;
	background-color: #d6ffd6;
}
</style>

